// pages/myLib/myLib.js
import { book } from '../../server/book';
import { file } from "../../server/file";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        bookList: []      // 所有加入书架的书本
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getJoinBook();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    /*
     * 返回上一页
     */
    returnMainPage() {
        wx.navigateBack({ delta: 1 });
    },

/*
     * 加入书架
     */
    joinInLib(e) {
        // e.stopPropagation()
        let bookId = e.currentTarget.dataset.bookid;
        // 先找到书本对应的对象
        let bookList = this.data.bookList;
        for (let idx = 0; idx < bookList.length; idx++) {
            if (bookList[idx].id == bookId) {
                // 判断调用加入还是取消加入的函数
                if (bookList[idx].isJoin) {
                    // 取消加入
                    book.noJoinInLib(bookId).then((res) => {
                        wx.showToast({
                            title: '取消加入书架成功',
                            icon: 'none',
                            duration: 1000
                        });
                        bookList[idx].isJoin = false;
                        this.setData({
                            bookList
                        });
                    }).catch((err) => {
                        wx.showToast({
                            title: '请检查网络连接',
                            icon: 'none',
                            duration: 1000
                        });
                    });
                } else {
                    // 加入书架
                    book.joinInLib(bookId).then((res) => {
                        wx.showToast({
                            title: '加入书架成功',
                            icon: 'none',
                            duration: 1000
                        });
                        bookList[idx].isJoin = true;
                        this.setData({
                            bookList
                        });
                    }).catch((err) => {
                        wx.showToast({
                            title: '请检查网络连接',
                            icon: 'none',
                            duration: 1000
                        });
                    });
                }
                break;
            }
        }
    },

    // 获取所有在书架中的书籍
    getJoinBook() {
        book.getJoinBook().then(async (res) => {
            let bookList = res.data.bookList;
            for (let i = 0; i < bookList.length; i++) {
                let imgUrl = await file.downloadPublicFile(bookList[i].imgId);
                console.log(imgUrl);
                bookList[i].imgUrl = imgUrl.data.url;
            }
            this.setData({
                bookList
            });
        }).catch((err) => {
            wx.showToast({
                title: '请检查网络连接',
                icon: 'none',
                duration: 1000
            });
        });
    },

    /*
     * 下拉加载更多书本
     */
    loadMore() {
        let lastId = this.data.bookList[this.data.bookList.length - 1].id;
        book.getJoinBook(lastId).then(async (res) => {
            let newBookList = res.data.bookList;
            for (let i = 0; i < newBookList.length; i++) {
                let imgUrl = await file.downloadPublicFile(newBookList[i].imgId);
                console.log(imgUrl);
                newBookList[i].imgUrl = imgUrl.data.url;
            }
            let oldBookList = this.data.bookList;
            let bookList = oldBookList.concat(newBookList);
            this.setData({
                bookList
            });
        }).catch((err) => {
            wx.showToast({
                title: '请检查网络连接',
                icon: 'none',
                duration: 1000
            });
        });
    },
    // 跳转到参考书内容页面
    jmpBookContent(e) {
        let item= e.currentTarget.dataset.item;
        console.log('item', item);
        wx.navigateTo({
            url: '/pages/bookContent/bookContent?book='+item.id,
        });
    }
})